<html>
  <head>
  <title>captureAgents.py</title>
  </head>
  <body>
  <h3>captureAgents.py (<a href="../captureAgents.py">original</a>)</h3>
  <hr>
  <pre>
<span style="color: green; font-style: italic"># captureAgents.py
# ----------------
# Licensing Information: Please do not distribute or publish solutions to this
# project. You are free to use and extend these projects for educational
# purposes. The Pacman AI projects were developed at UC Berkeley, primarily by
# John DeNero (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# For more info, see http://inst.eecs.berkeley.edu/~cs188/sp09/pacman.html

</span><span style="color: darkred">"""
  Interfaces for capture agents and agent factories
"""

</span><span style="color: blue; font-weight: bold">from </span>game <span style="color: blue; font-weight: bold">import </span>Agent
<span style="color: blue; font-weight: bold">import </span>distanceCalculator
<span style="color: blue; font-weight: bold">from </span>util <span style="color: blue; font-weight: bold">import </span>nearestPoint
<span style="color: blue; font-weight: bold">import </span>util

<span style="color: green; font-style: italic"># Note: the following class is not used, but is kept for backwards
# compatibility with team submissions that try to import it.
</span><span style="color: blue; font-weight: bold">class </span>AgentFactory<span style="font-weight: bold">:
  </span><span style="color: red">"Generates agents for a side"

  </span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>isRed<span style="font-weight: bold">, **</span>args<span style="font-weight: bold">):
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>isRed <span style="font-weight: bold">= </span>isRed

  <span style="color: blue; font-weight: bold">def </span>getAgent<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>index<span style="font-weight: bold">):
    </span><span style="color: red">"Returns the agent for the provided index."
    </span>util<span style="font-weight: bold">.</span>raiseNotDefined<span style="font-weight: bold">()

</span><span style="color: blue; font-weight: bold">class </span>RandomAgent<span style="font-weight: bold">( </span>Agent <span style="font-weight: bold">):
  </span><span style="color: darkred">"""
  A random agent that abides by the rules.
  """
  </span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">( </span><span style="color: blue">self</span><span style="font-weight: bold">, </span>index <span style="font-weight: bold">):
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>index <span style="font-weight: bold">= </span>index

  <span style="color: blue; font-weight: bold">def </span>getAction<span style="font-weight: bold">( </span><span style="color: blue">self</span><span style="font-weight: bold">, </span>state <span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">return </span>random<span style="font-weight: bold">.</span>choice<span style="font-weight: bold">( </span>state<span style="font-weight: bold">.</span>getLegalActions<span style="font-weight: bold">( </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>index <span style="font-weight: bold">) )

</span><span style="color: blue; font-weight: bold">class </span>CaptureAgent<span style="font-weight: bold">(</span>Agent<span style="font-weight: bold">):
  </span><span style="color: darkred">"""
  A base class for capture agents.  The convenience methods herein handle
  some of the complications of a two-team game.

  Recommended Usage:  Subclass CaptureAgent and override chooseAction.
  """

  </span><span style="color: green; font-style: italic">#############################
  # Methods to store key info #
  #############################

  </span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">( </span><span style="color: blue">self</span><span style="font-weight: bold">, </span>index<span style="font-weight: bold">, </span>timeForComputing <span style="font-weight: bold">= .</span><span style="color: red">1 </span><span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Lists several variables you can query:
    self.index = index for this agent
    self.red = true if you're on the red team, false otherwise
    self.agentsOnTeam = a list of agent objects that make up your team
    self.distancer = distance calculator (contest code provides this)
    self.observationHistory = list of GameState objects that correspond
        to the sequential order of states that have occurred so far this game
    self.timeForComputing = an amount of time to give each turn for computing maze distances
        (part of the provided distance calculator)
    """
    </span><span style="color: green; font-style: italic"># Agent index for querying state
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>index <span style="font-weight: bold">= </span>index

    <span style="color: green; font-style: italic"># Whether or not you're on the red team
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>red <span style="font-weight: bold">= </span><span style="color: blue">None

    </span><span style="color: green; font-style: italic"># Agent objects controlling you and your teammates
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>agentsOnTeam <span style="font-weight: bold">= </span><span style="color: blue">None

    </span><span style="color: green; font-style: italic"># Maze distance calculator
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>distancer <span style="font-weight: bold">= </span><span style="color: blue">None

    </span><span style="color: green; font-style: italic"># A history of observations
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>observationHistory <span style="font-weight: bold">= []

    </span><span style="color: green; font-style: italic"># Time to spend each turn on computing maze distances
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>timeForComputing <span style="font-weight: bold">= </span>timeForComputing

    <span style="color: green; font-style: italic"># Access to the graphics
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>display <span style="font-weight: bold">= </span><span style="color: blue">None

  </span><span style="color: blue; font-weight: bold">def </span>registerInitialState<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    This method handles the initial setup of the
    agent to populate useful fields (such as what team
    we're on).

    A distanceCalculator instance caches the maze distances
    between each pair of positions, so your agents can use:
    self.distancer.getDistance(p1, p2)
    """
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>red <span style="font-weight: bold">= </span>gameState<span style="font-weight: bold">.</span>isOnRedTeam<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>index<span style="font-weight: bold">)
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>distancer <span style="font-weight: bold">= </span>distanceCalculator<span style="font-weight: bold">.</span>Distancer<span style="font-weight: bold">(</span>gameState<span style="font-weight: bold">.</span>data<span style="font-weight: bold">.</span>layout<span style="font-weight: bold">)

    </span><span style="color: green; font-style: italic"># comment this out to forgo maze distance computation and use manhattan distances
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>distancer<span style="font-weight: bold">.</span>getMazeDistances<span style="font-weight: bold">()

    </span><span style="color: blue; font-weight: bold">import </span>__main__
    <span style="color: blue; font-weight: bold">if </span><span style="color: red">'_display' </span><span style="color: blue; font-weight: bold">in </span>dir<span style="font-weight: bold">(</span>__main__<span style="font-weight: bold">):
      </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>display <span style="font-weight: bold">= </span>__main__<span style="font-weight: bold">.</span>_display

  <span style="color: blue; font-weight: bold">def </span>final<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>observationHistory <span style="font-weight: bold">= []

  </span><span style="color: blue; font-weight: bold">def </span>registerTeam<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>agentsOnTeam<span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Fills the self.agentsOnTeam field with a list of the
    indices of the agents on your team.
    """
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>agentsOnTeam <span style="font-weight: bold">= </span>agentsOnTeam

  <span style="color: blue; font-weight: bold">def </span>observationFunction<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: red">" Changing this won't affect pacclient.py, but will affect capture.py "
    </span><span style="color: blue; font-weight: bold">return </span>gameState<span style="font-weight: bold">.</span>makeObservation<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>index<span style="font-weight: bold">)

  </span><span style="color: blue; font-weight: bold">def </span>debugDraw<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>cells<span style="font-weight: bold">, </span>color<span style="font-weight: bold">, </span>clear<span style="font-weight: bold">=</span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">):

    </span><span style="color: blue; font-weight: bold">if </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>display<span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">from </span>captureGraphicsDisplay <span style="color: blue; font-weight: bold">import </span>PacmanGraphics
      <span style="color: blue; font-weight: bold">if </span>isinstance<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>display<span style="font-weight: bold">, </span>PacmanGraphics<span style="font-weight: bold">):
        </span><span style="color: blue; font-weight: bold">if not </span>type<span style="font-weight: bold">(</span>cells<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">is </span>list<span style="font-weight: bold">:
          </span>cells <span style="font-weight: bold">= [</span>cells<span style="font-weight: bold">]
        </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>display<span style="font-weight: bold">.</span>debugDraw<span style="font-weight: bold">(</span>cells<span style="font-weight: bold">, </span>color<span style="font-weight: bold">, </span>clear<span style="font-weight: bold">)

  </span><span style="color: blue; font-weight: bold">def </span>debugClear<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">if </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>display<span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">from </span>captureGraphicsDisplay <span style="color: blue; font-weight: bold">import </span>PacmanGraphics
      <span style="color: blue; font-weight: bold">if </span>isinstance<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>display<span style="font-weight: bold">, </span>PacmanGraphics<span style="font-weight: bold">):
        </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>display<span style="font-weight: bold">.</span>clearDebug<span style="font-weight: bold">()

  </span><span style="color: green; font-style: italic">#################
  # Action Choice #
  #################

  </span><span style="color: blue; font-weight: bold">def </span>getAction<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Calls chooseAction on a grid position, but continues on half positions.
    If you subclass CaptureAgent, you shouldn't need to override this method.  It
    takes care of appending the current gameState on to your observation history
    (so you have a record of the game states of the game) and will call your
    choose action method if you're in a state (rather than halfway through your last
    move - this occurs because Pacman agents move half as quickly as ghost agents).

    """
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>observationHistory<span style="font-weight: bold">.</span>append<span style="font-weight: bold">(</span>gameState<span style="font-weight: bold">)

    </span>myState <span style="font-weight: bold">= </span>gameState<span style="font-weight: bold">.</span>getAgentState<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>index<span style="font-weight: bold">)
    </span>myPos <span style="font-weight: bold">= </span>myState<span style="font-weight: bold">.</span>getPosition<span style="font-weight: bold">()
    </span><span style="color: blue; font-weight: bold">if </span>myPos <span style="font-weight: bold">!= </span>nearestPoint<span style="font-weight: bold">(</span>myPos<span style="font-weight: bold">):
      </span><span style="color: green; font-style: italic"># We're halfway from one position to the next
      </span><span style="color: blue; font-weight: bold">return </span>gameState<span style="font-weight: bold">.</span>getLegalActions<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>index<span style="font-weight: bold">)[</span><span style="color: red">0</span><span style="font-weight: bold">]
    </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">return </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>chooseAction<span style="font-weight: bold">(</span>gameState<span style="font-weight: bold">)

  </span><span style="color: blue; font-weight: bold">def </span>chooseAction<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Override this method to make a good agent. It should return a legal action within
    the time limit (otherwise a random legal action will be chosen for you).
    """
    </span>util<span style="font-weight: bold">.</span>raiseNotDefined<span style="font-weight: bold">()

  </span><span style="color: green; font-style: italic">#######################
  # Convenience Methods #
  #######################

  </span><span style="color: blue; font-weight: bold">def </span>getFood<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Returns the food you're meant to eat. This is in the form of a matrix
    where m[x][y]=true if there is food you can eat (based on your team) in that square.
    """
    </span><span style="color: blue; font-weight: bold">if </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>red<span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">return </span>gameState<span style="font-weight: bold">.</span>getBlueFood<span style="font-weight: bold">()
    </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">return </span>gameState<span style="font-weight: bold">.</span>getRedFood<span style="font-weight: bold">()

  </span><span style="color: blue; font-weight: bold">def </span>getFoodYouAreDefending<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Returns the food you're meant to protect (i.e., that your opponent is
    supposed to eat). This is in the form of a matrix where m[x][y]=true if
    there is food at (x,y) that your opponent can eat.
    """
    </span><span style="color: blue; font-weight: bold">if </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>red<span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">return </span>gameState<span style="font-weight: bold">.</span>getRedFood<span style="font-weight: bold">()
    </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">return </span>gameState<span style="font-weight: bold">.</span>getBlueFood<span style="font-weight: bold">()

  </span><span style="color: blue; font-weight: bold">def </span>getCapsules<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">if </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>red<span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">return </span>gameState<span style="font-weight: bold">.</span>getBlueCapsules<span style="font-weight: bold">()
    </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">return </span>gameState<span style="font-weight: bold">.</span>getRedCapsules<span style="font-weight: bold">()

  </span><span style="color: blue; font-weight: bold">def </span>getCapsulesYouAreDefending<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">if </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>red<span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">return </span>gameState<span style="font-weight: bold">.</span>getRedCapsules<span style="font-weight: bold">()
    </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">return </span>gameState<span style="font-weight: bold">.</span>getBlueCapsules<span style="font-weight: bold">()

  </span><span style="color: blue; font-weight: bold">def </span>getOpponents<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Returns agent indices of your opponents. This is the list of the numbers
    of the agents (e.g., red might be "1,3,5")
    """
    </span><span style="color: blue; font-weight: bold">if </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>red<span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">return </span>gameState<span style="font-weight: bold">.</span>getBlueTeamIndices<span style="font-weight: bold">()
    </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">return </span>gameState<span style="font-weight: bold">.</span>getRedTeamIndices<span style="font-weight: bold">()

  </span><span style="color: blue; font-weight: bold">def </span>getTeam<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Returns agent indices of your team. This is the list of the numbers
    of the agents (e.g., red might be the list of 1,3,5)
    """
    </span><span style="color: blue; font-weight: bold">if </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>red<span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">return </span>gameState<span style="font-weight: bold">.</span>getRedTeamIndices<span style="font-weight: bold">()
    </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">return </span>gameState<span style="font-weight: bold">.</span>getBlueTeamIndices<span style="font-weight: bold">()

  </span><span style="color: blue; font-weight: bold">def </span>getScore<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Returns how much you are beating the other team by in the form of a number
    that is the difference between your score and the opponents score.  This number
    is negative if you're losing.
    """
    </span><span style="color: blue; font-weight: bold">if </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>red<span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">return </span>gameState<span style="font-weight: bold">.</span>getScore<span style="font-weight: bold">()
    </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">return </span>gameState<span style="font-weight: bold">.</span>getScore<span style="font-weight: bold">() * -</span><span style="color: red">1

  </span><span style="color: blue; font-weight: bold">def </span>getMazeDistance<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>pos1<span style="font-weight: bold">, </span>pos2<span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Returns the distance between two points; These are calculated using the provided
    distancer object.

    If distancer.getMazeDistances() has been called, then maze distances are available.
    Otherwise, this just returns Manhattan distance.
    """
    </span>d <span style="font-weight: bold">= </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>distancer<span style="font-weight: bold">.</span>getDistance<span style="font-weight: bold">(</span>pos1<span style="font-weight: bold">, </span>pos2<span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">return </span>d

  <span style="color: blue; font-weight: bold">def </span>getPreviousObservation<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Returns the GameState object corresponding to the last state this agent saw
    (the observed state of the game last time this agent moved - this may not include
    all of your opponent's agent locations exactly).
    """
    </span><span style="color: blue; font-weight: bold">if </span>len<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>observationHistory<span style="font-weight: bold">) == </span><span style="color: red">1</span><span style="font-weight: bold">: </span><span style="color: blue; font-weight: bold">return </span><span style="color: blue">None
    </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">: </span><span style="color: blue; font-weight: bold">return </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>observationHistory<span style="font-weight: bold">[-</span><span style="color: red">2</span><span style="font-weight: bold">]

  </span><span style="color: blue; font-weight: bold">def </span>getCurrentObservation<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Returns the GameState object corresponding this agent's current observation
    (the observed state of the game - this may not include
    all of your opponent's agent locations exactly).
    """
    </span><span style="color: blue; font-weight: bold">return </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>observationHistory<span style="font-weight: bold">[-</span><span style="color: red">1</span><span style="font-weight: bold">]

  </span><span style="color: blue; font-weight: bold">def </span>displayDistributionsOverPositions<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>distributions<span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Overlays a distribution over positions onto the pacman board that represents
    an agent's beliefs about the positions of each agent.

    The arg distributions is a tuple or list of util.Counter objects, where the i'th
    Counter has keys that are board positions (x,y) and values that encode the probability
    that agent i is at (x,y).

    If some elements are None, then they will be ignored.  If a Counter is passed to this
    function, it will be displayed. This is helpful for figuring out if your agent is doing
    inference correctly, and does not affect gameplay.
    """
    </span>dists <span style="font-weight: bold">= []
    </span><span style="color: blue; font-weight: bold">for </span>dist <span style="color: blue; font-weight: bold">in </span>distributions<span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">if </span>dist <span style="font-weight: bold">!= </span><span style="color: blue">None</span><span style="font-weight: bold">:
        </span><span style="color: blue; font-weight: bold">if not </span>isinstance<span style="font-weight: bold">(</span>dist<span style="font-weight: bold">, </span>util<span style="font-weight: bold">.</span>Counter<span style="font-weight: bold">): </span><span style="color: blue; font-weight: bold">raise </span>Exception<span style="font-weight: bold">(</span><span style="color: red">"Wrong type of distribution"</span><span style="font-weight: bold">)
        </span>dists<span style="font-weight: bold">.</span>append<span style="font-weight: bold">(</span>dist<span style="font-weight: bold">)
      </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
        </span>dists<span style="font-weight: bold">.</span>append<span style="font-weight: bold">(</span>util<span style="font-weight: bold">.</span>Counter<span style="font-weight: bold">())
    </span><span style="color: blue; font-weight: bold">if </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>display <span style="font-weight: bold">!= </span><span style="color: blue">None </span><span style="color: blue; font-weight: bold">and </span><span style="color: red">'updateDistributions' </span><span style="color: blue; font-weight: bold">in </span>dir<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>display<span style="font-weight: bold">):
      </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>display<span style="font-weight: bold">.</span>updateDistributions<span style="font-weight: bold">(</span>dists<span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
      </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>_distributions <span style="font-weight: bold">= </span>dists <span style="color: green; font-style: italic"># These can be read by pacclient.py


</span><span style="color: blue; font-weight: bold">class </span>TimeoutAgent<span style="font-weight: bold">( </span>Agent <span style="font-weight: bold">):
  </span><span style="color: darkred">"""
  A random agent that takes too much time. Taking
  too much time results in penalties and random moves.
  """
  </span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">( </span><span style="color: blue">self</span><span style="font-weight: bold">, </span>index <span style="font-weight: bold">):
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>index <span style="font-weight: bold">= </span>index

  <span style="color: blue; font-weight: bold">def </span>getAction<span style="font-weight: bold">( </span><span style="color: blue">self</span><span style="font-weight: bold">, </span>state <span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">import </span>random<span style="font-weight: bold">, </span>time
    time<span style="font-weight: bold">.</span>sleep<span style="font-weight: bold">(</span><span style="color: red">2.0</span><span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">return </span>random<span style="font-weight: bold">.</span>choice<span style="font-weight: bold">( </span>state<span style="font-weight: bold">.</span>getLegalActions<span style="font-weight: bold">( </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>index <span style="font-weight: bold">) )
</span>
  </pre>
  </body>
  </html>
  